package com.ecnu.web.controller.tool;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.dto.ProFileDTO;
import com.ecnu.system.domain.vo.IncubationVO;
import com.ecnu.system.service.IIncubationFactoryService;
import com.ecnu.system.service.IProjectFileService;
import com.ecnu.system.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动资料接口
 */
@Api(tags = "活动资料接口")
@RestController
@RequestMapping("/material")
@Validated
public class MaterialController extends BaseController {

    @Autowired
    private IProjectService projectService;
    @Autowired
    private IProjectFileService projectFileService;
    @Autowired
    private IIncubationFactoryService incubationFactoryService;

    @ApiOperation(value = "查询活动资料列表")
    @PostMapping("/list")
    public TableDataInfo list(@Validated @RequestBody ProFileDTO dto) {
        LoginUser loginUser = getLoginUser();
        return projectFileService.getListForResult(dto,loginUser);
    }

    @ApiOperation(value = "活动资料同步")
    @PostMapping("/syncEdit")
    public AjaxResult sync(@Validated @RequestBody List<String> projectFile) {
        try {
            if (CollectionUtils.isEmpty(projectFile)) {
                return AjaxResult.success("没有需要同步的数据");
            }
            //  获取活动资料(活动处理信息，存储地址）
            List<IncubationVO> maps = projectFileService.getlistToResult(projectFile);
            if (CollectionUtils.isEmpty(maps)) {
                return AjaxResult.success("没有需要同步的数据");
            }
            // 同步数据
            return incubationFactoryService.editList(maps);
        }catch (Exception e){
            logger.error("活动资料同步失败："+e.getMessage());
            String[] parts = e.getMessage().split("\\\\");
            String lastPart = parts[parts.length - 1];
            return AjaxResult.error("同步失败，请联系管理员处理："+lastPart);
        }
    }

}
